package com.sunwayworld.basemodule.rm.equipment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sunwayworld.basemodule.basedata.operationrecord.bean.LimsOperationrecordBean;
import com.sunwayworld.basemodule.basedata.operationrecord.service.LimsOperationrecordService;
import com.sunwayworld.framework.i18n.I18nHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.rm.equipment.dao.LimsEquipmentDao;
import com.sunwayworld.basemodule.rm.equipment.bean.LimsEquipmentBean;
import com.sunwayworld.basemodule.rm.equipment.service.LimsEquipmentService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Repository
@GikamBean
public class LimsEquipmentServiceImpl implements LimsEquipmentService {

    @Autowired
    private LimsEquipmentDao limsEquipmentDao;

    @Autowired
    private LimsOperationrecordService limsOperationrecordService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsEquipmentDao getDao() {
        return limsEquipmentDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsEquipmentBean limsEquipment = jsonWrapper.parseUnique(LimsEquipmentBean.class);
        limsEquipment.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsEquipment);
        return limsEquipment.getId();
    }

    @Override
    public JSONObject updateEquipState(RestJsonWrapperBean jsonWrapperBean) {
        JSONObject req = new JSONObject();

        String equipState = jsonWrapperBean.getParamValue("equipState");
        String remark = jsonWrapperBean.getParamValue("remark");
        LocalDateTime operationTime = LocalDateTime.parse(jsonWrapperBean.getParamValue("operationTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        List<LimsEquipmentBean> updateBeans = jsonWrapperBean.parse(LimsEquipmentBean.class);
        if (!updateBeans.isEmpty() && equipState != null) {
            List<LimsOperationrecordBean> operationRecords = new ArrayList<>();

            updateBeans.forEach(i -> {
                LimsOperationrecordBean operationRecord = new LimsOperationrecordBean();
                operationRecord.setId(ApplicationContextHelper.getNextIdentity());
                operationRecord.setOperationDesc(I18nHelper.getMessage("T_EQUIPMENT.TIP.CHANGE.EQUIPSTATE"));
                operationRecord.setValueBeforeOperation(i.getEquipState());
                operationRecord.setValueAfterOperation(equipState);
                operationRecord.setRemark(remark);
                operationRecord.setBusinessId(i.getId());
                operationRecord.setOperationTime(operationTime);

                operationRecords.add(operationRecord);
                i.setEquipState(equipState);
            });
            getDao().update(updateBeans, "equipState");

            limsOperationrecordService.getDao().insert(operationRecords);
        }

        return req;
    }
}
